<div class="content container">

    <!-- container -->
    <section class="subject">
        <p class="count">
            共<span class="count0">{$total}</span>题： 
            单选题<span class="count1">{$arr.danxuan}</span> 
            多选题<span class="count2">{$arr.duoxuan}</span> 
            判断题<span class="count3">{$arr.panduan}</span> 
            填空题<span class="count4">{$arr.tiankong}</span> 
            主观题<span class="count5">{$arr.zhuguan}</span> 
        </p>

        <div class="box row">
            <div class="col-lg-6">
                <h2>已录入自建题库</h2>
                <section class="exam-list">
                    <volist name="list" id="vo" key="k">
                        <h3><if condition="$vo['type'] eq '1'">单选题<elseif condition="$vo['type'] eq '2'" />多选题<elseif condition="$vo['type'] eq '3'" />判断题<elseif condition="$vo['type'] eq '4'" />填空题<elseif condition="$vo['type'] eq '5'" />主观题</if>
                    </h3>
                    <!-- 题目列表 -->
                    <div class="exam-list-li">
                        <a class="close" data-tid="{$vo.id}">×</a>
                        <!-- 考试-题目 -->
                        <div class="exam-list-li-text">
                            {$vo['content']}
                        </div>
                        <!-- 考试-题目 End -->
                        <if condition="$vo['type'] eq '1' or $vo['type'] eq '2'">
                        <!-- 考试-答案 -->
                        <div class="exam-list-li-answer">
                            <volist name="vo['answeroptions']" id="voo" key="k">
                            <div class="radio">
                                {$voo}
                            </div>
                            </volist>
                        </div>
                        <elseif condition="$vo['type'] eq '3'" />
                        <div class="exam-list-li-answer">
                            <div class="radio">A.正确</div>
                            <div class="radio">B.错误</div>
                        </div>
                        
                        </if>
                        <!-- 考试-答案 End -->
                    </div>
                    <!-- 题目列表 End -->
                    </volist>

                    <div id="pages" class="page">{$page}</div>

                  
                </section>
            </div>

            <!-- 输入题目 -->
            <div class="col-lg-6">
                <h2>输入题目</h2>
                <form method="post" action="index.php?g=User&m=Teacher&a=savequestion" name="form1">
                <section class="subject-add">
                    <div class="form-group clearfix">
                        <label for="inputUser" class="col-sm-3">题型设置：</label>
                        <div class="col-sm-9">
                            <select class="form-control" id="subject-type">
                                <option value="0">客观题</option>
                                <option value="5">主观题</option>
                            </select>
                            <select class="form-control" id="subject-ti">
                                <option value="1">单选</option>
                                <option value="2">多选</option>
                                <option value="3">判断</option>
                                <option value="4">填空</option>
                            </select>
                            <input name="type" id="typehidden" type="hidden" value="">
                        </div>
                    </div>
                    <div class="col-sm-12">
                        <h3>题目：</h3>
                        <textarea class="form-control timu" rows="3" name="content" placeholder="可以输入多行文本"></textarea>
                    </div>
                    <div class="col-sm-12 subject-option">
                        <a class="pull-right text-success answer-add">添加答案</a>
                        <h3>备选答案：</h3>
                        <div class="answer-option">
                        </div>
                    </div>
                    <div class="col-sm-12 subject-answer">
                        <h3>正确答案：</h3>
                        <select class="form-control" id="answer-selece">
                        </select>
                        <input class="hide" type="text" name="answer" id="answer">
                    </div>
                    
                    <div class="col-sm-12 text-right"><a class="btn btn-success submit submit11">写入题目</a></div>
                </section>
                </form>
            </div>
            <!-- 输入题目 End -->
            
        </div>
    </section>
    <!-- container End -->

</div>
<script src="__PUBLIC__/dist/js/jquery.js"></script>
<script>
    $(function () {
        
        //禁用Enter键表单自动提交  
        document.onkeydown = function(event) {  
            var target, code, tag;  
            if (!event) {  
                event = window.event; //针对ie浏览器  
                target = event.srcElement;  
                code = event.keyCode;  
                if (code == 13) {  
                    tag = target.tagName;  
                    if (tag == "TEXTAREA") { return true; }  
                    else { return false; }  
                }  
            }  
            else {  
                target = event.target; //针对遵循w3c标准的浏览器，如Firefox  
                code = event.keyCode;  
                if (code == 13) {  
                    tag = target.tagName;  
                    if (tag == "INPUT") { return false; }  
                    else { return true; }  
                }  
            }  
        }; 
        
        // var count0 = 0;     //总计
        // var count1 = 0;     //单选题
        // var count2 = 0;     //多选题
        // var count3 = 0;     //判断题
        // var count4 = 0;     //填空题
        // var count5 = 0;     //主观题

        //alphabet= String.fromCharCode(64 + parseInt(填写数字));

        var answerNum = 0;
        $(".answer-add").on("click",function(){
            if(answerNum == 26) return alert("超过限制");

            var val = $(".answer-option").find("div").eq(answerNum - 1).find("input").val();
            if(!val) return alert("答案请不要留空");

            answer_option();
        })
        function en26(num){
            return alphabet= String.fromCharCode(65 + parseInt(num));
        }
        function answer_option(){
            var html = '<div><label>'+ en26(answerNum) +'、</label><input name="answeroption['+en26(answerNum)+']" type="text"></div>';
            $(".answer-option").append(html);
            $("#answer-selece").append('<option value="'+en26(answerNum)+'">' + en26(answerNum) + '</option>');
            answerNum++;
        }
        answer_option();

        // 主客观题选择
        $("#subject-type").on("change",function(){
            if(Number($(this).val())){
                $(".subject-option").hide();
                $(".subject-answer").hide();
                $("#subject-ti").hide();
            }else{
                $(".subject-answer").show();
                $("#subject-ti").show();
                if($("#subject-ti").val() == 3){
                    $(".subject-option").hide();
                }else if($("#subject-ti").val() == 2){
                    $(".subject-option").hide();
                }else{
                    $(".subject-option").show();
                }
            }
        })
        // 客观题类型选择
        $("#subject-ti").on("change",function(){
            var obj1 = $(".subject-option");
            var obj2 = $(".subject-answer");
            var val = Number($(this).val());
            
            obj1.show();
            obj2.find("select").show();
            obj2.find("input").hide();
            $(".answer-option").html("");
            $("#answer-selece").html("");
            $("#answer-selece").removeAttr("multiple");
            $(".subject-answer h3 small").remove();
            if(val == 1){
                //单选题
                obj1.find("a").show();

                answerNum = 0;
                answer_option();

            }else if(val == 2){
                //多选题
                obj1.find("a").show();

                answerNum = 0;
                answer_option();

                $("#answer-selece").attr("multiple","multiple");
                $(".subject-answer h3").append("<small>多选可按Ctrl键或Shift键选择，或者拖拉选取</small>")
            }else if(val == 3){
                //判断题
                obj1.hide();
                obj1.find("a").hide();

                answerNum = 0;
                $("#answer-selece").html('<option value="A">A、 正确</option><option value="B">B、 错误</option>')

            }else if(val == 4){
                //填空题
                obj1.hide();
                obj2.find("select").hide();
                obj2.find("input").show();
            }
        })

        // 写入按钮
        $(".submit").on("click",function(){
            if(!$(".timu").val()) return alert("请输入题目");
            var data = new Object;
            data["title"] = $(".timu").val();

            //备选答案列表
            function bei(){
                var arr = new Array;
                var obj = $(".answer-option").find("input");
                for(var i = 0;i < obj.length;i++){
                    if(!obj.eq(i).val()) return arr;
                    var val = obj.eq(i).val();
                    arr.push(val);
                }
                return arr;
            }

            // 判断题型
            if(!Number($("#subject-type").val())){
                var type = $("#subject-ti").val();
                data["type"] = type;
                
                var obj1 = $(".subject-option");
                var obj2 = $(".subject-answer");

                if(type == 0){
                    //单选题
                    data["beixuan"] = bei();

                    if( $(".subject-option").find("input").eq($("#answer-selece").val()).val()){
                        data["answer"] = $("#answer-selece").val();
                    }else{
                        return alert("请输入答案");
                    }
                    
                }else if(type == 1){
                    //多选题
                    data["beixuan"] = bei();

//                    var l = $("#answer-selece").val();
//                    for(var i=0;i<l.length;i++){
//                        if( $(".subject-option").find("input").eq(l[i]).val()){
//                            data["answer"] = $("#answer-selece").val();
//                        }else{
//                            return alert("请输入答案1");
//                        }
//                    }
                }else if(type == 2){
                    //判断题
                    data["answer"] = $("#answer-selece").val();
                }else if(type == 3){
                    //填空题
                    data["answer"] = obj2.find("input").val();
                }
            }

            console.log(data);
            submit(data)
        })
        function submit(data){
            $.ajax({data,url:"##"})
        }
        $(".submit11").on("click",function(){
            var data = new Object;
            if($("#subject-type").val()=='0'){
                $("#typehidden").val($("#subject-ti").val()) ;
            }else{
                $("#typehidden").val($("#subject-type").val()) ;
            }
        if($("#subject-ti").val()=='2'){
            var l = $("#answer-selece").val();
            for(var i=0;i<l.length;i++){
                if( $(".subject-option").find("input").eq(i).val()){
                    data["answer"] = $("#answer-selece").val();
                }else{
                    return alert("请输入答案");
                }
            }
            $("#answer").val(JSON.stringify(data["answer"]));
        }else if($("#subject-ti").val()=='1' || $("#subject-ti").val()=='3'){
            $("#answer").val($("#answer-selece").val());
        }
          form1.submit() ;
        })
        
         $(".close").on("click",function(){ 
         var aa = $(this).data('tid') ;
           $.ajax({
           type: "POST",
           url: "index.php?g=User&m=Teacher&a=deletequestion",
           data: "aa="+aa,
           dataType: "json",
           success: function(resp)
           {
               if(resp.code=='0000'){
                   alert('删除成功') ;
                   window.location.reload() ;
               }else{
                   alert('删除失败') ;
               }
            }        
        });
         })
    })
</script>